package com.qriver.distributedlock.controller;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class DemoController {

    private Logger logger = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private RedissonClient redissonClient;

    @RequestMapping("redissonLock")
    public String testLock() {
        logger.debug("进入testLock()方法；");
        //“order"表示业务模块

        RLock rLock = redissonClient.getLock("order");
        try{
            if(rLock.tryLock(1,40, TimeUnit.SECONDS)){
                logger.debug("获取到锁了；");
                Thread.sleep(60 * 1000);
            }else{
                logger.debug("获取锁失败了；");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.debug("方法执行完成；");
        return "方法执行完成";
    }

}
